﻿#include "page_puass.h"
#include "ui_page_puass.h"
#include <cmath>

//================================ Коля ================================
/*!
 *param [in] ryad - ряд распределения геометрического, гипергеометрического или особого распределения
 *возвращаемое значение - математические характеристики:
 *	- мат. ожидание
 *	- дисперсия
 *	- средне квадратичное отклонение
 */
std::vector<double> mathCharacteristics(std::vector<double> &ryad);

int factorial(unsigned int n)
{
    return n == 0 ? 1 : n*factorial(n-1);
}

double puas( int n, double p, int k )
{
    double a = p*n;
    double res = pow(a,	k)*pow(2.7182818284,-a)/factorial(k);

    return res;
}

std::vector<double> puas_rad( int n, double p)
{
    std::vector<double> res;
    for(int k=0;k<=n;++k)
    {
        res.push_back(puas(n, p, k ));
    }
    return res;

}

//================================================================

page_puass::page_puass(QWidget *parent)
	: page_clean(parent)
{
	ui = new Ui::page_puassClass();
	ui->setupUi(this);

    QObject::connect(ui->checkBox, SIGNAL(clicked(bool)), ui->checkBox_disp, SLOT(setChecked(bool)));
    QObject::connect(ui->checkBox, SIGNAL(clicked(bool)), ui->checkBox_f_raspr, SLOT(setChecked(bool)));
    QObject::connect(ui->checkBox, SIGNAL(clicked(bool)), ui->checkBox_sr_kv_otkl, SLOT(setChecked(bool)));
    QObject::connect(ui->checkBox, SIGNAL(clicked(bool)), ui->checkBox_math_o, SLOT(setChecked(bool)));
    QObject::connect(ui->checkBox, SIGNAL(clicked(bool)), ui->checkBox_rad_raspr, SLOT(setChecked(bool)));
}

page_puass::~page_puass()
{
    delete ui;
}

void page_puass::inputPuass(unsigned int *n, double *p)
{
    (*n) = ui->input_n->value();
    (*p) = ui->input_p->value();
}

void page_puass::on_calc_btn_clicked()
{
    unsigned int n;
    double p;
    inputPuass(&n,&p);
    std::vector<double> v = puas_rad(n,p);
    std::vector<double> params = mathCharacteristics(v);
    if(ui->checkBox_rad_raspr->checkState()==Qt::Checked)
    {
        show_Rad(v);
    }
    if(ui->checkBox_math_o->checkState()==Qt::Checked)
    {
		emit appendToLog(QString("M(X) = ")+QString::number(n*p));
        //emit appendToLog(QString("M(X) = ")+QString::number(params[0]));
    }
    if(ui->checkBox_disp->checkState()==Qt::Checked)
    {
        //emit appendToLog(QString("D(X) = ")+QString::number(params[1]));
		emit appendToLog(QString("D(X) = ")+QString::number(n*p));
    }
    if(ui->checkBox_sr_kv_otkl->checkState()==Qt::Checked)
    {
        emit appendToLog(QString("&#963;(X) = ")+QString::number( pow(n*p,0.5) ));
    }
    if(ui->checkBox_f_raspr->checkState()==Qt::Checked)
    {
        show_Graph(v);
    }
    //if(ui->checkBox_mng->checkState()==Qt::Checked)           //calc_Mng();
    //if(ui->checkBox_graph->checkState()==Qt::Checked)
    emit appendToLog("<hr />");
}
